﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using proRFID_DiemDanh.Chung;
using proRFID_DiemDanh.GiaoVien;
using proRFID_DiemDanh.MonHoc;
using proRFID_DiemDanh.PhongHoc;
using proRFID_DiemDanh.Lop;

namespace proRFID_DiemDanh.LichDay
{
    public partial class SuaLichDay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request["ngay"]))
                {
                    Response.Redirect("/LichDay/LichDay.aspx");
                    return;
                }
                Load_DDL_GiaoVien();
                Load_DDL_Phong();
                HienThi();
            }
        }
        void Load_DDL_GiaoVien()
        {
            DataTable dt = new DataTable();
            dt =mdLichDay.sp_GiaoVien_DDL(1);
            ddl_GiaoVien.DataSource = dt;
            ddl_GiaoVien.DataValueField = "MAGV";
            ddl_GiaoVien.DataTextField = "HOTENGV";
            ddl_GiaoVien.DataBind();
        }
        void Load_DDL_Phong()
        {
            DataTable dt = new DataTable();
            dt = mdPhongHoc.PhongHoc_Select(0);
            ddl_PhongHoc.DataSource = dt;
            ddl_PhongHoc.DataValueField = "MAPHONG";
            ddl_PhongHoc.DataTextField = "TENPHONG";
            ddl_PhongHoc.DataBind();
        }
        void HienThi()
        {
            string[] date = null;
            date=Request["ngay"].ToString().Split('/');
            txt_Ngay.Text = date[2]+"-"+date[1]+"-"+date[0];
            ddl_BuoiHoc.SelectedValue = Request["buoi"];
            ddl_GiaoVien.SelectedValue = Request["magv"];
            ddl_PhongHoc.SelectedValue=Request["maphong"];
            txt_Monhoc.Text = Request["Tenmh"];
            txt_Lop.Text = Request["Tenlop"];
        }

        protected void Imgcmd_SuaLichDay_Click(object sender, ImageClickEventArgs e)
        {
            string[] tmp = Request["ngay"].ToString().Split('/');
            string date = tmp[2] + "-" + tmp[1] + "-" + tmp[0];

            if (txt_Ngay.Text == date && ddl_BuoiHoc.SelectedValue == Request["buoi"] && ddl_GiaoVien.SelectedValue == Request["magv"]
               && ddl_PhongHoc.SelectedValue == Request["maphong"])
            {
                mdMain.OKBox(this, "Cập nhật dữ liệu thành công", "/LichDay/LichDay.aspx");
                return;
            }

            DataTable dt = new DataTable();
            dt = mdLichDay.sp_LichDay_Update(txt_Ngay.Text, Int16.Parse(ddl_BuoiHoc.SelectedValue), Int16.Parse(ddl_GiaoVien.SelectedValue), 
                                             Int16.Parse(ddl_PhongHoc.SelectedValue),Convert.ToDateTime(Request["ngay"]), 
                                             Int16.Parse(Request["buoi"]), Int16.Parse(Request["magv"]), Int16.Parse(Request["maphong"]));
            if (dt.Rows[0]["ErrCode"].ToString() == "0")
            {
                mdMain.OKBox(this, dt.Rows[0]["ErrMsg"].ToString(), "/LichDay/LichDay.aspx");
            }
            else
            {
                mdMain.OKCancelBox(this, dt.Rows[0]["ErrMsg"].ToString() + ". Bạn có muốn thử lại không?", "/LichDay/LichDay.aspx");                
            }
        }

        protected void Imgcmd_ThemPhong_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/PhongHoc/ThemPhong.aspx");
        }

        protected void Imgcmd_ThemMon_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/MonHoc/ThemMonHoc.aspx");
        }

        protected void Imgcmd_ThemLop_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/Lop/ThemLop.aspx");
        }
    }
}